package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.dao.IOrderDetailDao;
import com.zhijiaoyun.model.OrderDetail;
import com.zhijiaoyun.model.dto.CourseDto;
import com.zhijiaoyun.model.dto.OrderDetailDto;
import com.zhijiaoyun.service.ICourseService;
import com.zhijiaoyun.service.IOrderDetailService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "orderDetailService")
public class OrderDetailServiceImpl implements IOrderDetailService {

	@Resource
	IOrderDetailDao orderDetailDao;

	@Resource
	ICourseService courseService;
	
	@Override
	public void addOrderDetail(OrderDetail detail) {
		orderDetailDao.insert(detail);
	}

	@Override
	public List<OrderDetail> getOrderDetailByOrderNum(String orderNum) {
		return orderDetailDao.selectByOrderNum(orderNum);
	}

	@Override
	public List<OrderDetailDto> getDtoByOrderNum(String orderNum) {
		List<OrderDetailDto> detailDtos = new ArrayList<OrderDetailDto>();
		List<OrderDetail> details = orderDetailDao.selectByOrderNum(orderNum);
		if (!StringUtils.isEmptyList(details)) {
			for (OrderDetail orderDetail : details) {
				if (orderDetail.getGoodsType() == 1) {
					CourseDto courseDto = courseService.getCourseDtoByCourseId(orderDetail.getGoodsId());
					if (courseDto != null) {
						OrderDetailDto detailDto = new OrderDetailDto();
						detailDto.setCourseDto(courseDto);
						detailDtos.add(detailDto);
					}
				}
			}
		}
		return detailDtos;
	}

}
